package com.cb.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.cb.admin.domain.UserInfo;
import com.cb.admin.mapper.IUserInfoMapper;
import com.cb.admin.service.IUserInfoService;

/**
 * 用户信息业务逻辑
 * 添加redis缓存，判断
 * @author Administrator
 *
 */
@Service
public class UserInfoServiceImpl implements IUserInfoService {
	
	private static Logger logger = Logger.getLogger(UserInfoServiceImpl.class.getName());
	
	@Autowired
	private IUserInfoMapper userInfoMapper;
	@Autowired
    private RedisTemplate redisTemplate;

	@Override
	public List<Map> query(UserInfo userInfo) {
		// TODO Auto-generated method stub
		return userInfoMapper.query(userInfo);	
	}

	@Override
	public int querySize(UserInfo userInfo) {
		// TODO Auto-generated method stub
		return userInfoMapper.querySize(userInfo);
	}

	/**
	 * 判断账号是否存在
	 */
	@Override
	public int queryByAccount(String account) {
		// TODO Auto-generated method stub
		return userInfoMapper.queryByAccount(account);
	}

	/**
	 * 登录限制方法使用
	 */
	@Override
	public UserInfo queryByAP(String account) {
		// TODO Auto-generated method stub
		return userInfoMapper.queryByAP(account);
	}

	/**
     * 获取UserInfo：
     * 如果缓存存在，从缓存中获取UserInfo信息
     * 如果缓存不存在，从 DB 中获取UserInfo信息，然后插入缓存
     */
	@Override
	public UserInfo queryById(int id) {
		// TODO Auto-generated method stub
		
		String key = "user_" + id;
        ValueOperations<String, UserInfo> operations = redisTemplate.opsForValue();
        // 缓存存在
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
        	UserInfo userInfo = operations.get(key);

        	logger.info("UserInfoServiceImpl.queryById() : 从缓存中获取了userInfo >> " + userInfo.toString());
            return userInfo;
        }
        
        UserInfo userInfo = userInfoMapper.queryById(id);
        
        // 插入缓存
        operations.set(key, userInfo);
        logger.info("UserInfoServiceImpl.queryById() : userInfo插入缓存 >> " + userInfo.toString());
		
		return userInfo;
	}

	@Override
	public int insert(UserInfo userInfo) {
		// TODO Auto-generated method stub
		return userInfoMapper.insert(userInfo);
	}

	/**
     * 更新userInfo逻辑：
     * 如果缓存存在，删除
     * 如果缓存不存在，不操作
     */
	@Override
	public void update(UserInfo userInfo) {
		// TODO Auto-generated method stub
		
		userInfoMapper.update(userInfo);
		
		String key = "user_" + userInfo.getId();
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            redisTemplate.delete(key);

            logger.info("UserInfoServiceImpl.update() : 从缓存中删除userInfo >> " + userInfo.toString());
        }
	}

	@Override
	public void deleteById(int id) {
		// TODO Auto-generated method stub

		userInfoMapper.deleteById(id);
		
		// 缓存存在，删除缓存
        String key = "user_" + id;
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            redisTemplate.delete(key);

            logger.info("CityServiceImpl.deleteById() : 从缓存中删除userInfo ID >> " + id);
        }
	}

	@Override
	public void updateState(int id,int state) {
		HashMap rep = new HashMap();
		rep.put("id", id);
		rep.put("state", state);
		// TODO Auto-generated method stub
		userInfoMapper.updateState(rep);
		
		String key = "user_" + id;
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            redisTemplate.delete(key);

            logger.info("UserInfoServiceImpl.updateState() : 从缓存中删除id >> " + id);
        }
	}

	@Override
	public List<String> queryUserRoleByName(String account) {
		// TODO Auto-generated method stub
		return userInfoMapper.queryUserRoleByName(account);
	}

	@Override
	public List<String> queryRoleMemuByName(List<String> roleId) {
		// TODO Auto-generated method stub
		return userInfoMapper.queryRoleMemuByName(roleId);
	}

	@Override
	public Map queryForMap(UserInfo userInfo) {
		// TODO Auto-generated method stub
		Map rep = new HashMap();
		List<Map> userInfolis = userInfoMapper.queryPeople(userInfo);
		int size = userInfoMapper.queryPeopleSize(userInfo); 
		rep.put("userInfolis", userInfolis);
		rep.put("size", size);
		return rep;
	}

	@Override
	public Map query4PointForMap(UserInfo userInfo) {
		// TODO Auto-generated method stub
		Map rep = new HashMap();
		List<Map> userInfolis = userInfoMapper.query4PointPeople(userInfo);
		int size = userInfoMapper.query4PointPeopleSize(userInfo); 
		rep.put("userInfolis", userInfolis);
		rep.put("size", size);
		return rep;
	}
}